import http from './http'

// 商家登录
export function sellerLogin(payload) {
  // payload: { username, password }
  return http.post('/user/sellerLogin', payload)
}

// 商家注册/入驻申请
export function sellerRegister(payload) {
  // payload: { shopName, contactName, phone, email, password }
  return http.post('/seller/register', payload)
}

// 获取商家信息
export function getSellerInfo() {
  return http.get('/seller/info')
}

// 更新商家信息
export function updateSellerInfo(payload) {
  return http.put('/seller/info', payload)
}

// 发送验证码
export function sendVerifyCode(phone) {
  return http.post('/seller/sendVerifyCode', { phone })
}

// 商家店铺信息
export function getShopInfo() {
  return http.get('/seller/shop')
}

// 更新店铺信息
export function updateShopInfo(payload) {
  return http.put('/seller/shop', payload)
}

// 查询日销售数据
export function queryDailySales(sellerId, year, month) {
  return http.get('/orders/queryDailySales', {
    params: { sellerId, year, month }
  })
}

// 查询月销售数据
export function queryMonthlySales(sellerId, year) {
  return http.get('/orders/queryMonthlySales', {
    params: { sellerId, year }
  })
}

// 根据商家ID查询商家信息
export function getSellerInfoById(sellerId) {
  return http.get('/seller/querySellerById', {
    params: { id: sellerId }
  })
}

// 获取商家分类
export function getSellerCategory(sellerId) {
  return http.get(`/category/getSellerCategory/${sellerId}`)
}

// 添加分类
export function addCategory(categoryAddDTO) {
  // 根据后端要求，使用GET请求并通过查询参数传递数据
  return http.get('/category/add', {
    params: categoryAddDTO
  })
}

// 更新分类
export function updateCategory(categoryUpdateDTO) {
  // 使用PUT请求并通过请求体传递数据
  return http.put('/category/update', categoryUpdateDTO)
}

// 删除分类
export function deleteCategory(categoryDeleteDTO) {
  // 使用DELETE请求并通过请求体传递数据
  return http.delete('/category/delete', {
    data: categoryDeleteDTO
  })
}

// 根据ID查询商家信息
export function querySellerById(id) {
  return http.get('/seller/querySellerById', {
    params: { id }
  })
}

// 增加商品浏览量
export function incrementProductViewCount(productId) {
  return http.post(`/product/incrementViewCount/${productId}`)
}

// 更新商家信息
export function updateMessage(sellerUpdateDTO) {
  return http.put('/seller/updateMessage', sellerUpdateDTO)
}

// 更新商家Logo
export function updateLogo(logo, sellerId) {
  const formData = new FormData()
  formData.append('logo', logo)
  formData.append('sellerId', sellerId)
  // 移除手动设置的Content-Type头，让浏览器自动设置正确的Content-Type（包括boundary信息）
  return http.put('/seller/updateLogo', formData)
}

// 申请成为商家
export function addSeller(sellerData) {
  return http.post('/seller/addSeller', sellerData)
}

// 上传商家Logo
export function uploadLogo(logoFile) {
  const formData = new FormData()
  formData.append('logo', logoFile)
  return http.post('/seller/getLogoUrl', formData, {
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  })
}

// 删除商品图片
export function deleteProductImage(imageId) {
  return http.delete(`/product/deleteImages?id=${imageId}`)
}
